package cn.car.service.impl;

import cn.car.domains.entity.VehicleEntryExitInfoEntity;
import cn.car.exception.CarAccessControlException;
import cn.car.service.VehicleEntryExitInfoService;
import cn.car.mapper.VehicleEntryExitInfoMapper;
import cn.hutool.core.date.DateTime;
import jakarta.annotation.Resource;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class VehicleEntryExitInfoServiceImpl implements VehicleEntryExitInfoService {
    @Resource
    private VehicleEntryExitInfoMapper vehicleEntryExitInfoMapper;
    @Override
    public VehicleEntryExitInfoEntity queryById(Long id) {
        return vehicleEntryExitInfoMapper.queryById(id);
    }

    @Override
    public Long insert(VehicleEntryExitInfoEntity vehicleEntity) {
        // 执行插入操作
        int rows;
        try {
            rows = vehicleEntryExitInfoMapper.insert(vehicleEntity);
        } catch (DuplicateKeyException e) {
            throw CarAccessControlException.DATE_EXIST;
        }

        if (rows != 1) {
            throw CarAccessControlException.INSERT_ERROR.newInstance("插入车辆失败");
        }
        return vehicleEntity.getId();
    }

    @Override
    public VehicleEntryExitInfoEntity queryVehicleByNumber(String vehicleNumber) {
        return vehicleEntryExitInfoMapper.queryVehicleByNumber(vehicleNumber);
    }

    @Override
    public void deleteVehicle(Long id) {
        // 校验ID是否存在
        if (id == null) {
            throw CarAccessControlException.PARAMS_ERROR.newInstance("删除id不能为空");
        }

        // 执行删除操作
        int rows = vehicleEntryExitInfoMapper.deleteById(id);

        if (rows != 1) {
            throw CarAccessControlException.DELETE_NOT_EXIST;
        }
    }

    @Override
    public void update(VehicleEntryExitInfoEntity updateEntryExitEntity) {
        vehicleEntryExitInfoMapper.updateById(updateEntryExitEntity);
    }

    @Override
    public List<VehicleEntryExitInfoEntity> compensateVehicleInfoUploadStatus(DateTime startTime, DateTime endTime) {
        return vehicleEntryExitInfoMapper.compensateVehicleInfoUploadStatus(startTime, endTime);
    }
}
